package cn.wolfcode.wolf2w.search.Controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domian.DestinationEs;
import cn.wolfcode.wolf2w.search.domian.StrategyEs;
import cn.wolfcode.wolf2w.search.domian.TravelEs;
import cn.wolfcode.wolf2w.search.domian.UserInfoEs;
import cn.wolfcode.wolf2w.search.service.IDestinationEsService;
import cn.wolfcode.wolf2w.search.service.IStrategyEsService;
import cn.wolfcode.wolf2w.search.service.ITravelEsService;
import cn.wolfcode.wolf2w.search.service.IUserInfoEsService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DataController {
    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IUserInfoEsService userInfoEsService;
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ITravelEsService travelEsService;
    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDestinationEsService destinationEsService;
    @Autowired
    private IDestinationService destinationService;

    /**
     * 将mongodb中的信息同步到es数据库，数据初始化
     * @return
     */
    @GetMapping("/dataIndex")  // 实现初始化需要先访问一下。
    public Object getStrategyEsData(){
        // 从mongodb数据获取数据
        List<Strategy> strategyList = strategyService.list();

        StrategyEs strategyEs = new StrategyEs();

        for (Strategy strategy : strategyList){
            // 将mongodb中查询到到的数据复制到es
            BeanUtils.copyProperties(strategy, strategyEs);
            // 保存数据到es数据库中
            strategyEsService.save(strategyEs);
        }

        // 从mongodb数据获取数据
        List<UserInfo> userInfoList = userInfoService.list();

        UserInfoEs userInfoEs = new UserInfoEs();

        for (UserInfo userInfo : userInfoList){
            // 将mongodb中查询到到的数据复制到es
            BeanUtils.copyProperties(userInfo, userInfoEs);
            // 保存数据到es数据库中
            userInfoEsService.save(userInfoEs);
        }

        // 从mongodb数据获取数据
        List<Travel> travelsList = travelService.list();

        TravelEs travelEs = new TravelEs();

        for (Travel travel : travelsList){
            // 将mongodb中查询到到的数据复制到es
            BeanUtils.copyProperties(travel, travelEs);
            // 保存数据到es数据库中
            travelEsService.save(travelEs);
        }

        // 从mongodb数据获取数据
        List<Destination> destinationsList = destinationService.list();

        DestinationEs destinationEs = new DestinationEs();

        for (Destination destination : destinationsList){
            // 将mongodb中查询到到的数据复制到es
            BeanUtils.copyProperties(destination, destinationEs);
            // 保存数据到es数据库中
            destinationEsService.save(destinationEs);
        }
        return "ok";
    }
}
